package com.mrslu.signkk.controller;

import com.mrslu.signkk.interceptor.PassToken;
import com.mrslu.signkk.interceptor.UserThread;
import com.mrslu.signkk.pojo.User;
import com.mrslu.signkk.result.Result;
import com.mrslu.signkk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author 卢斌
 */
@RequestMapping(value = "/user")
@CrossOrigin
@ResponseBody
@Controller
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 用户登录与注册
     */
    @PassToken
    @GetMapping(value = "/login")
    public Result<?> userLogin(@RequestParam("code") String code) {
        String userToken = userService.userLogin(code);
        return Result.ok(userToken);
    }


    /**
     * 更新用户基本信息
     */
    @PutMapping(value = "/user_info")
    public Result<?> updateUserInfo(@RequestBody User user) {
        Integer orgId = UserThread.orgId();
        Integer userId = UserThread.userId();
        user.setId(userId);
        user.setOrgId(orgId);
        user = userService.updateUserDetail(user);
        return Result.ok(user);
    }


    /**
     * 获取用户基本信息
     */
    @GetMapping(value = "/user_info")
    public Result<?> getUserInfo() {
        Integer orgId = UserThread.orgId();
        Integer userId = UserThread.userId();
        User user = userService.getUserDetail(orgId, userId);
        return Result.ok(user);
    }



}
